﻿using System;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using System.Windows.Input;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Looptribe.Moodlin8.Controller;
using Looptribe.Moodlin8.Model;
using Looptribe.Moodlin8.Model.Api;
using Microsoft.Practices.ServiceLocation;
using Windows.UI.Core;

namespace Looptribe.Moodlin8.ViewModel
{
    public abstract class LoadingViewModel : ViewModelBase
    {
        private bool _isLoading;
        public bool IsLoading
        {
            get { return _isLoading; }
            protected set { Set(() => IsLoading, ref _isLoading, value); }
        }

        protected ApplicationController Controller
        {
            get { return ServiceLocator.Current.GetInstance<ApplicationController>(); }
        }

        protected IMoodleApiService Api
        {
            get { return ServiceLocator.Current.GetInstance<IMoodleApiService>(); }
        }

        public WebserviceInfo Info
        {
            get { return Controller.Info; }
        }

        public User CurrentUser
        {
            get { return Controller.CurrentUser; }
        }

        private readonly ObservableCollection<NavSelector> _navItems = new ObservableCollection<NavSelector>();
        public ObservableCollection<NavSelector> NavItems { get { return _navItems; } }

        public ICommand LogoutCommand { get; private set; }
        public ICommand RefreshCommand { get; private set; }
        public ICommand NavigateCommand { get; private set; }

        protected LoadingViewModel()
        {
            LogoutCommand = new RelayCommand(() => Controller.Logout());
            RefreshCommand = new RelayCommand(() => Refresh());
            NavigateCommand = new RelayCommand<NavSelector>(a => a.Execute());

            NavItems.Add(new NavSelector("Enrolled Courses", () => Controller.GotoCourses()));
            NavItems.Add(new NavSelector("Next Assignments", () => Controller.GotoNextAssignments(null)));
            NavItems.Add(new NavSelector("Corrected Assignments", () => Controller.GotoCorrectedAssignments(null)));
            NavItems.Add(new NavSelector("Uploads", () => Controller.GotoUploads()));
            //NavItems.Add(new NavSelector("Messages", () => Controller.GotoMessages()));
            //NavItems.Add(new NavSelector(?separator?));
            NavItems.Add(new NavSelector("Home", () => Controller.GotoHub()));
        }

        protected async void RunAsync(DispatchedHandler callback)
        {
            await ServiceLocator.Current.GetInstance<CoreDispatcher>().RunAsync(CoreDispatcherPriority.Normal, callback);
        }

        public virtual Task Refresh()
        {
            return Task.FromResult<object>(null);
        }
    }
}
